/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package android.reasons;

import android.app.Activity;
import android.app.ContextoAndroid;
import android.app.Mensaje;
import android.entidades.Voz;
import android.graphics.Typeface;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.sql.UsuarioPensamiento;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Locale;

/**
 *
 * @author efraingutierrez
 */
public class Notificacion extends Activity implements TextToSpeech.OnInitListener
{ 
    private LinearLayout abs;
    private TextView tvHeader,tvRazon;
    private TextToSpeech textToSpeech;
    private String msj = "EMPTY";
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ContextoAndroid.context = this;
        this.setContentView(R.layout.notificacion);
        abs = (LinearLayout)this.findViewById(R.id.absNot);
        abs.setBackgroundResource(R.drawable.turquesa);
        tvHeader = (TextView)this.findViewById(R.id.tvNotificacion);
        tvRazon = (TextView)this.findViewById(R.id.tvRazon);
        Typeface font = Typeface.createFromAsset(getAssets(), "abril.ttf");
        for (int i = 0; i < abs.getChildCount(); i++) 
        {
            View v = abs.getChildAt(i);
            if (v instanceof EditText) 
            {
                ((EditText)v).setTypeface(font);
            } else if (v instanceof TextView) {
                ((TextView)v).setTypeface(font);
            } 
        }
        textToSpeech = new TextToSpeech(ContextoAndroid.context, this);
        Bundle extras = getIntent().getExtras();
        if (extras != null) 
        {
            String value = extras.getString("valor");
            int num = Integer.parseInt(value);
            try
            {
                UsuarioPensamiento info = android.entidades.Data.getData().get(num);
                tvRazon.setText(info.Pensamiento);
                tvHeader.setText(tvHeader.getText() + "" + (num+1));
                msj = "Razón Número "+(num+1) + " , "+info.Pensamiento;
                
            }
            catch(Exception e)
            {
                tvRazon.setText("");
                tvHeader.setText("Resetenado Razones...");
                msj = "Volver al Menu";
            }
        }
        else Mensaje.Mostrar("NULOTE");
        convertTextToSpeech(msj);
    }
    
   
    private void convertTextToSpeech(String text) 
    {
        if (null == text || "".equals(text)) 
        {
           text = "Please give some input.";
           convertTextToSpeech(text);
        }
        if(Voz.getVoz().equals("1"))
            textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
    
    @Override
     public void onDestroy() {
         super.onDestroy();
          textToSpeech.shutdown();
     }
    
    @Override
    public void onInit(int status) 
    {
        if (status == TextToSpeech.SUCCESS) 
        {
            Locale loc = new Locale("es", "MX");
            int result = textToSpeech.setLanguage(loc);
            if (result == TextToSpeech.LANG_MISSING_DATA  || result == TextToSpeech.LANG_NOT_SUPPORTED) 
            {
                Mensaje.Mostrar("Error de voz :(" );
            } 
            else 
            {
                convertTextToSpeech(msj);
            }
        } 
        else 
        {
            Mensaje.Mostrar("la Voz fallo :( !");
        }
    }
}
